# 定义 ModelSerializer

from rest_framework import serializers

from books.models import BookInfo

def bread_gt_twenty(value):
    if int(value) < 20:
        raise serializers.ValidationError('入库图书  bread>20')
    return value

# 图书的序列化器
class BookInfoSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    btitle = serializers.CharField(
        max_length=20,
        min_length=5,
        error_messages={
            'required': '必填',
            'max_length': "书名不能大于20",
            'min_length': "书名不能小于5"
        },

    )
    bpub_date = serializers.DateField(label='发布日期', required=False)
    # bread = serializers.IntegerField(label='阅读量', required=False, validators=[bread_gt_twenty])
    bread = serializers.IntegerField(label='阅读量', required=False)
    bcomment = serializers.IntegerField(label='评论量', required=False)

    # 1:n
    heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    # 自定义校验--单个校验
    # def validate_btitle(self, value):
    #     if 'drf' in value:
    #         # assert 0, '书名不能包含DRf'
    #         raise serializers.ValidationError('书名不能包含DRf')
    #     return value

    # 多个   字段
    # def validate(self, attrs):
    #     bread = attrs['bread']
    #     bcomment = attrs['bcomment']
    #     if bread < bcomment:
    #         raise serializers.ValidationError('阅读量必须大于阅读量')
    #
    #     return attrs

    def create(self, validated_data):
        return BookInfo.objects.create(**validated_data)

    def update(self, instance, validated_data):

        instance.btitle = validated_data.get('btitle', instance.btitle)
        instance.bpub_date = validated_data.get('bpub_date', instance.bpub_date)
        instance.bread = validated_data.get('bread', instance.bread)
        instance.bcomment = validated_data.get('bcomment', instance.bcomment)
        instance.save()
        return instance

        # results = BookInfo.objects.filter(pk=instance.id).update(**validated_data)
        # # results 1 0
        # return BookInfo.objects.get(pk=instance.id)

class HeroInfoSerializer(serializers.Serializer):
    """英雄数据序列化器"""
    GENDER_CHOICES = (
        (0, 'male'),
        (1, 'female')
    )
    id = serializers.IntegerField(label='ID', read_only=True)
    hname = serializers.CharField(label='名字', max_length=20)
    hgender = serializers.ChoiceField(choices=GENDER_CHOICES, label='性别', required=False)
    hcomment = serializers.CharField(label='描述信息', max_length=200, required=False, allow_null=True)

    # 1.int ID
    # hbook = serializers.PrimaryKeyRelatedField(read_only=True)

    # 2. str ====ORM模型类"__str_"
    # hbook = serializers.StringRelatedField()

    # 3.序列化器对象
    # hbook = BookInfoSerializer()


from books.serializers import BookInfoSerializer, HeroInfoSerializer
from books.models import BookInfo, HeroInfo
# hero = HeroInfo.objects.get(pk=3)
# ser = HeroInfoSerializer(instance=hero)
# ser.data


# data_dict = {'btitle':"python Spider","bread":10}
# ser = BookInfoSerializer(data=data_dict)
# ser.is_valid()


# class BookInfoSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = BookInfo
#         fields = "__all__"

